<template>
    <d2-container>
        <!-- 搜索表头 -->
        <template slot="header">
            <el-form :inline="true" :model="searchForm" ref="searchForm" size="small"
                v-if="showHeader" class="searchHeader">

                <el-form-item label="编号" prop="clientId">
                    <el-input v-model="searchForm.clientId" placeholder="请输入编号" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="getDataTable">
                        <d2-icon name="search" />
                        查询
                    </el-button>
                    <el-button @click="handleFormReset">
                        <d2-icon name="refresh" />
                        重置
                    </el-button>
                </el-form-item>

            </el-form>
        </template>

        <!-- 表格表头 -->
        <div class="table-header">
            <div>
                <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addOrUpdate()">添加</el-button>
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
            </div>
            <div>
                <ec-prompt-button content="刷新" icon="el-icon-refresh" @click="getDataTable()"></ec-prompt-button>
                <ec-prompt-button content="搜索" icon="el-icon-search" @click="changeHeader()"></ec-prompt-button>
                <ec-prompt-button content="导入" icon="el-icon-download"></ec-prompt-button>
                <ec-prompt-button content="导出" icon="el-icon-upload2"></ec-prompt-button>
                <ec-prompt-button content="分享" icon="el-icon-share"></ec-prompt-button>
            </div>
        </div>


        <!-- 表格表体 -->
        <ec-table :data="tabledata"  v-loading="loading">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <el-table-column prop="clientId" header-align="center" align="center" show-overflow-tooltip label="编号">
            </el-table-column>
            <el-table-column prop="resourceIds" header-align="center" align="center" show-overflow-tooltip label="资源">
            </el-table-column>
            <el-table-column prop="clientSecret" header-align="center" align="center" show-overflow-tooltip label="密钥">
            </el-table-column>
            <el-table-column prop="scope" header-align="center" align="center" show-overflow-tooltip label="域">
            </el-table-column>
            <el-table-column prop="authorizedGrantTypes" header-align="center" align="center" show-overflow-tooltip
                label="授权类型">
            </el-table-column>
            <el-table-column prop="webServerRedirectUri" header-align="center" align="center" show-overflow-tooltip
                label="重定向地址">
            </el-table-column>
            <el-table-column prop="authorities" header-align="center" align="center" show-overflow-tooltip label="权限">
            </el-table-column>
            <el-table-column prop="accessTokenValidity" header-align="center" align="center" show-overflow-tooltip
                label="令牌时效">
            </el-table-column>
            <el-table-column prop="refreshTokenValidity" header-align="center" align="center" show-overflow-tooltip
                label="刷新时效">
            </el-table-column>
            <el-table-column prop="autoapprove" header-align="center" align="center" show-overflow-tooltip label="自动放行">
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="120" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="addOrUpdate(scope.row.clientId)">修改
                    </el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.clientId)">删除</el-button>
                </template>
            </el-table-column>

        </ec-table>

        <!-- 表格表尾 -->
        <template slot="footer">
            <!-- <el-button type="primary" size="mini" :loading="uploading" style="float:left;">
                <d2-icon name="cloud-upload" />
                Upload log data
            </el-button> -->

            <ec-pagination :page="page" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            </ec-pagination>
        </template>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataTable"></add-or-update>
    </d2-container>
</template>

<script>
    import * as clientService from '@/api/upms/client'
    import AddOrUpdate from './add-or-update'

    export default {
        name: "system-client-index",
        data() {
            return {
                searchForm: {
                    clientId: ''
                },
                page: {
                    page: 1,
                    limit: 10,
                    total: 0
                },
                showHeader: true,
                tabledata: [],
                loading: false,
                addOrUpdateVisible: false
            }
        },
        components: {
            AddOrUpdate
        },
        mounted() {
            this.getDataTable()
        },
        methods: {
            // 获取数据列表
            getDataTable() {
                this.loading = true
                let query = {
                    page: this.page.page,
                    limit: this.page.limit,
                    clientId: this.searchForm.clientId
                };
                clientService.getclientlist(query).then(data => {
                    this.tabledata = data.list;
                    this.page.total = data.totalCount;
                    this.loading = false;
                }).catch((res) => {
                    this.loading = false;
                });
            },
            //切换页数
            handleSizeChange(val) {
                this.page.limit = val;
                this.getDataTable();
            },
            // 翻页
            handleCurrentChange(val) {
                this.page.page = val;
                this.getDataTable();
            },
            // 切换显示搜索表头
            changeHeader() {
                this.showHeader = !this.showHeader
            },
            // 重置搜索表单
            handleFormReset() {
                this.$refs.searchForm.resetFields()
            },
            // 新增 / 修改
            addOrUpdate(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 删除
            deleteHandle(id) {
                if (id) {
                    this.$confirm("确认删除？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        clientService.delclient(id).then(() => {
                            this.getDataTable();
                        });
                    });
                }
            }
        }
    }
</script>